from MultiFunc import MultiFunc
from QQBot import Robot
from Logger import Logger
from StatuStore import StatusStore
import threading
import random
import re
import requests
import ddddocr
import time

CONTROQID = # 请在这里填写控制qq机器人的qq号

if __name__ == '__main__':
    logger = Logger() # 实例化日志类
    status_store = {} # 用于存储状态的字典

    logger.DebugLog('====================开始运行========================')
    # 功能列表
    function_map = {
        '翻译': {'function': MultiFunc.translate, 'need_second': True, 'desc': '请输入您要翻译的内容~'},
        '上传图片': {'function': MultiFunc.uploadImage, 'need_second': True, 'desc': '请发送图片过来吧~'},
        'ocr': {'function': MultiFunc.imageOCR, 'need_second': True, 'desc': '请发送图片过来吧~'},
        '监控日志': {'function': MultiFunc.monitor_crawler, 'need_second': False}
    }


    def choiceFunction(store_obj: StatusStore):
        res = ''
        if function_map.get(store_obj.funcName):
            res = function_map.get(store_obj.funcName)['function'](store_obj.msg)
        return res



    bot = Robot()  # 实例化QQ机器人

    while True:
        bot.revMsg()
        if bot.msg['user_id'] == CONTROQID and bot.msg['message_type'] == 'private':
            if bot.msg['message'].strip().lower().startswith('cmd'):
                try:
                    funcName = bot.msg['message'].strip().split('\n')[0].split()[1]
                    store_obj = StatusStore(from_qq=CONTROQID, is_cmd=True, funcName=funcName)
                    func_info = function_map.get(funcName)
                    if not func_info:
                        res = '指令[{}]暂不支持'.format(funcName)
                    elif func_info.get('need_second'):
                        res = '收到你的指令：{}\n{}'.format(funcName, func_info.get(
                            'desc') or '已进入对应状态, 请继续发送详细内容')
                        # 添加或更新记录
                        status_store[CONTROQID] = store_obj
                    else:
                        res = '请求结果为：\n' + str(choiceFunction(store_obj))
                        status_store.pop(CONTROQID, '')
                except:
                    res = 'cmd与命令之间请加空格'
            else:
                res = '请先发送指令哦...'
                store_obj = status_store.get(CONTROQID)
                if store_obj and store_obj.is_cmd:
                    store_obj.msg = bot.msg['message']
                    res = '请求结果为：\n' + str(choiceFunction(store_obj))
                    status_store.pop(CONTROQID, '')
        elif bot.msg['message_type'] == 'private':
            res = bot.ai_talk()
        elif bot.msg['message_type'] == 'group':
            continue
        else:
            continue

        bot.sdMsg(res, bot.msg['user_id'], 'private')





